import {
	useUserStore
} from '@/stores/user.js'
const store = useUserStore();
import QQMapWX from '../libs/qqmap-wx-jssdk.min.js'
let qqmapsdk = new QQMapWX({
	key: 'YTQBZ-5FYRD-MSP4L-PIV3W-MTK22-OPBKQ'
});

export default function getLocation(useDemo) {
	return new Promise((resolve, reject) => {
		function getLocation() {
			uni.getLocation({
				type: 'gcj02',
				success: function(result) {
					resolve(result)
					let {
						latitude,
						longitude
					} = result;
					qqmapsdk.reverseGeocoder({
						location: {
							latitude,
							longitude
						},
						success: res => {
							let {
								city,
								district,
								nation,
								province,
								street
							} = res.result.address_component;
							store.setAddress({
								latitude,
								longitude,
								city,
								district,
								nation,
								province,
								street
							})
						},
						fail: (err) => {
							console.log(err);
						}
					})
				},
				fail: (err) => {
					reject(err)
				}
			});
		}
		getLocation();

		/* uni.getSetting({
			success: (res) => {
				let authStatus = res.authSetting['scope.userLocation'];
				if (!authStatus) {
					uni.showModal({
						title: '授权失败',
						content: '需要获取您当前的位置，请在设置界面打开相关权限',
						success: (res) => {
							if (res.confirm) {
								uni.openSetting({
									success(res) {
										console.log()
										if (res.authSetting[
												'scope.userLocation']) {
											getLocation();
										} else {
											uni.showToast({
												title: '授权失败',
												icon: 'none'
											})
										}
									}
								})
							}
						}
					})
				} else {
					getLocation();
				}
			}
		}) */
	})
}